#pragma once

struct SwsContext;
struct AVCodecContext;
struct AVCodecContext;
struct AVFormatContext;
typedef struct AVFrame AVFrame;
typedef struct AVPacket AVPacket;

class CVideoDecoder
{
public:
	CVideoDecoder();
	virtual ~CVideoDecoder();

	bool Initialize(const char* szFormat, int iWidth, int iHeight, int bFlip, int bActivateDInterlace);
	bool Terminate(bool bCodecOpen);
	bool Decode(const unsigned char* pInBuffer, unsigned long nInBufSize, 
					unsigned char* pOutBuffer, unsigned long nOutBufSize, 
					unsigned long nDestWidth, unsigned long nDestHeight, int stride, bool bDecodeToRGB, int *pbFrameFinished);
private:
	
	int					m_nVideoStream;
	AVFormatContext*	m_pFormatCtx;
	SwsContext*			m_pScale_context;
	AVFrame*				m_pFrame; 
	AVFrame*				m_pFrameRGB;
	AVFrame*				m_pDIFrame;
	int					m_bFlip;
	int					m_bActivateDInterlace;
	AVCodecContext*	m_pCodecCtx;
	unsigned long		m_nDestWidth;
	unsigned long		m_nDestHeight;
	bool					m_bFirstFrame;

	int  CreatePacket(const unsigned char* pInBuffer, unsigned long nInBufSize, AVPacket* pPacket);
	void LinearDeinterlace(unsigned char *pBufferIn, unsigned long nBufferSize, unsigned long nWidth, unsigned long nHeight, unsigned long nStride);
};